<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>promiseAPI</title>
</head>
<body>
<script>
    // all 全部 Promise.all() 如果都成功就正常返回,如果有一个失败,那么都会认为是失败的
    // race 赛跑 Promise.race() 返回的也是一个promise对象, race方法谁先有结果,就返回谁,无论成功还是失败(成功就走resolve,失败的会在reject中返回)
    // resolve 成功回调
    // reject 失败回调



    //returns = Promise.all(param)
    //@param values An iterable of Promises.可迭代的接口数据 [promise,promise]
    //@returns A new Promise. 返回一个新的promise对象

    {
        // Promise.all() 如果都成功就正常返回,如果有一个失败,那么都会认为是失败的
        // 里面的promise实例都是并行的,需要等到所有的promise都完成之后才会有结果
        /*let p1 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                resolve("p1");
            },1000)
        })
        let p2 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                resolve("p2");
            },3000)
        })
        let p3 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                // resolve("p3");
                reject("p3");
            },1000)
        })
        let pAll = Promise.all([p1,p2,p3]);
        pAll.then((res)=>{
            console.log(res)
        },err=>{
            console.log("err==>",err)
        });
        console.log(pAll);*/
        //当p1p2p3都执行完成后,pAll的状态变成已成功, 返回结果是参数的顺序
        //当p1p2p3中有任意一个失败,pAll会直接返回rejected, pAll的状态都是失败
    }

    {//Promise.race()  返回的也是一个promise对象, race方法谁先有结果,就返回谁,无论成功还是失败(成功就走resolve,失败的会在reject中返回)
        let p1 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                // resolve("p1");
                reject("p1");
            },1000)
        })
        let p2 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                resolve("p2");
            },3000)
        })
        let p3 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                resolve("p3");
                // reject("p3");
            },2000)
        })
        let pRace = Promise.race([p1,p2,p3]);
        pRace.then((res)=>{
            console.log(res)
        },err=>{
            console.log("err==>",err)
        });
        console.log(pRace)
    }


    {
        let p1 = Promise.resolve("let p1 = Promise.resolve");
        // let p3 = Promise.reject("let p1 = Promise.resolve");
        //p1完全等价于p2
        let p2 = new Promise((resolve, reject)=>{
            resolve("let p1 = Promise.resolve");
        })

        p1.then(res=>{
            // console.log(res);
        })
    }
    {
        //then catch finally
        let p1 = new Promise((resolve) => {
            resolve()
        })
        p1.then(res => {

        })
    }
</script>
</body>
</html>